/**
 * 
 */
package com.study.jvm;

import java.text.DecimalFormat;

/**
 * @author Administrator
 * @date 2019年4月21日
 */
public class JvmDemo01 {

	//-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags
	public static void main(String[] args) throws InterruptedException {
		System.out.println("分配前...");
		jvmInfo();
		//byte[] bytes = new byte[30*1024*1024];  //Java heap space
		byte[] bytes = new byte[1*1024*1024];
		Thread.sleep(3000);
		System.out.println("分配后...");
		jvmInfo();
	}

	
	/**
	 * @return 
	 */
	private static void jvmInfo() {
		// 当前运行环境最大堆内存
		System.out.println("maxMemory(M): " + toM(Runtime.getRuntime().maxMemory()));
		// 当前运行环境空闲内存
		System.out.println("freeMemory(M): " + toM(Runtime.getRuntime().freeMemory()));
		// 获取当前使用多少内存
		System.out.println("totalMemory(M): " + toM(Runtime.getRuntime().totalMemory()));
	}
	
	private static String toM(long memory){
		float num = (float) memory / (1024*1024);
		DecimalFormat decimalFormat = new DecimalFormat("0.00");
		String s = decimalFormat.format(num);
		return s;
	}
}
